package code.google.gui.adapter.util;

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import code.google.gui.fragments.DummyFragment1;
import code.google.gui.fragments.menu.MenuFragment;

import com.example.metamusicgui.R;

public abstract class MenuContext {
	
	protected Context context;
	private static MenuFragment menu;
	private static long itemIDMenu;
	private static int ITEM_ID_COUNTER = 0;

	public MenuContext(Context context){
		this.context = context;
		menu = getMenu();
	}
	
	private MenuFragment getMenu() {
		if(menu == null){
			menu = new MenuFragment();
			itemIDMenu = getItemID();
		}
		
		return menu;
	}

	protected void setDummyData(Fragment frag, String name) {
		Bundle bundle = new Bundle();
		bundle.putString(DummyFragment1.ARG_SECTION_NAME, name);
		frag.setArguments(bundle);
	}
	
	protected int getItemID(){
		int result = ITEM_ID_COUNTER++;
		return result;
	}
	
	public Fragment getItem(int position){
		if(position == 0){
			return menu;
		}
		else{
			 return getItemHook(position);
		}
	}
	
	public int getCount(){
		return 1 + getCountHook();
	}
	
	public CharSequence getPageTitle(int position){
		if(position == 0){
			Locale l = Locale.getDefault();
			Resources res = context.getResources();
			return res.getString(R.string.title_menu).toUpperCase(l);
		}
		else{
			return getPageTitleHook(position);
		}
	}
	
	public long getItemID(int position){
		if(position == 0)
			return itemIDMenu;
		else{
			return getItemIDHook(position);
		}
	}
	
	public void onRegistrateFragments(Activity context){
		menu.registrateFragmentAt(context);
		onRegistrateFragmentsHook(context);
	}
	
	public void onDeregistrateFragments(Activity context){
		menu.deregistrateFragmentFrom(context);
		onDeregistrateFragmentsHook(context);
	}
	
	protected abstract void onDeregistrateFragmentsHook(Activity context2);
	protected abstract void onRegistrateFragmentsHook(Activity context);
	
	protected abstract Fragment getItemHook(int position);

	protected abstract int getCountHook();

	protected abstract CharSequence getPageTitleHook(int position);

	protected abstract long getItemIDHook(int position);	
}
